<template>
  <el-dialog :title="!dataForm.houseId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="140px">
      <el-form-item label="租金"
                    prop="housePrice">
        <el-input v-model="dataForm.housePrice"
                  placeholder="租金"></el-input>
      </el-form-item>
      <el-form-item label="方式"
                    prop="houseWay">
        <!-- <el-input v-model="dataForm.houseWay" placeholder="方式(1.整租2.合租)"></el-input> -->
        <el-radio-group v-model="dataForm.houseWay">
          <el-radio :label="1">整租</el-radio>
          <el-radio :label="2">合租</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="房屋名"
                    prop="houseName">
        <el-input v-model="dataForm.houseName"
                  placeholder="房屋名"></el-input>
      </el-form-item>
      <el-form-item label="朝向"
                    prop="houseAspect">
        <!-- <el-input v-model="dataForm.houseAspect" placeholder="朝向(1.东2.西3.南4.北)"></el-input> -->
        <el-radio-group v-model="dataForm.houseAspect">
          <el-radio-button label="1">东</el-radio-button>
          <el-radio-button label="2">西</el-radio-button>
          <el-radio-button label="3">南</el-radio-button>
          <el-radio-button label="4">北</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="地址"
                    prop="houseAddress">
        <el-input v-model="dataForm.houseAddress"
                  placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="面积(m2)"
                    prop="houseArea">
        <el-input v-model="dataForm.houseArea"
                  placeholder="面积(m2)"></el-input>
      </el-form-item>
      <el-form-item label="状态"
                    prop="status">
        <!-- <el-input v-model="dataForm.status" placeholder="状态(0.未租 1.已租)"></el-input> -->
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">未租</el-radio>
          <el-radio :label="1">已租</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="户型"
                    prop="houseDesc">
        <!-- <el-input v-model="dataForm.houseDesc" placeholder="户型(如1室1厅)"></el-input> -->
        <el-radio-group v-model="dataForm.houseDesc">
          <el-radio-button label="一室一厅"></el-radio-button>
          <el-radio-button label="两室一厅"></el-radio-button>
          <el-radio-button label="三室一厅"></el-radio-button>
          <el-radio-button label="三室两厅"></el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认图片"
                    prop="defaultImg">
        <!-- <el-input v-model="dataForm.defaultImg" placeholder="默认图片"></el-input> -->
        <template slot-scope="scope">
          <img :src="dataForm.defaultImg"
               width="100px"
               height="100px"
               alt="">
          {{dataForm.defaultImg}}
          <el-upload action="http://localhost:8888/api/house/renthouse/upload"
                     class="upload-demo"
                     :on-success="uploadFile"
                     :on-remove="handleRemove"
                     :file-list="fileList"
                     list-type="picture">
            <el-button size="small"
                       type="primary">点击上传</el-button>
            <div slot="tip"
                 class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
          </el-upload>
        </template>
      </el-form-item>
      <el-form-item label="花钱置顶"
                    prop="rentSort">
        <el-input v-model="dataForm.rentSort"
                  placeholder="花钱置顶"></el-input>
      </el-form-item>
      <el-form-item label="置顶期限/月"
                    prop="deadline">
        <el-input v-model="dataForm.deadline"
                  placeholder="置顶期限/月"></el-input>
      </el-form-item>
      <el-form-item label="发布租房人"
                    prop="userId">
        <el-input v-model="dataForm.userId"
                  placeholder="发布租房人"></el-input>
      </el-form-item>
      <el-form-item label="创建时间"
                    prop="createTime">
        <!-- <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input> -->
        <el-date-picker v-model="dataForm.createTime"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        houseId: 0,
        housePrice: '',
        houseWay: null,
        houseName: '',
        houseAspect: '',
        houseAddress: '',
        houseArea: '',
        status: 0,
        houseDesc: '',
        defaultImg: '',
        rentSort: '',
        deadline: '',
        userId: 0,
        createTime: ''
      },
      fileList: [],
      dataRule: {
        housePrice: [
          { required: true, message: '租金不能为空', trigger: 'blur' }
        ],
        houseWay: [
          { required: true, message: '方式(1.整租2.合租)不能为空', trigger: 'blur' }
        ],
        houseName: [
          { required: true, message: '房屋名不能为空', trigger: 'blur' }
        ],
        houseAspect: [
          { required: true, message: '朝向(1.东2.西3.南4.北)不能为空', trigger: 'blur' }
        ],
        houseAddress: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        houseArea: [
          { required: true, message: '面积(m2)不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态(0.未租 1.已租)不能为空', trigger: 'blur' }
        ],
        houseDesc: [
          { required: true, message: '户型(如1室1厅)不能为空', trigger: 'blur' }
        ],
        defaultImg: [
          { required: true, message: '默认图片不能为空', trigger: 'blur' }
        ],
        rentSort: [
          { required: true, message: '花钱置顶不能为空', trigger: 'blur' }
        ],
        deadline: [
          { required: true, message: '置顶期限/月不能为空', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '发布租房人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    uploadFile (response, file, fileList) {
      console.log(fileList)
      // this.dataForm.defaultImg = this.dataForm.defaultImg+","+res.url
      let imgurl = ''
      for (const key in fileList) {
        imgurl = imgurl + ',' + fileList[key].response.url
      }
      this.dataForm.defaultImg = imgurl
      // this.fileList.push({name:'图片',url:res.url})
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
      let imgurl = ''
      for (const key in fileList) {
        imgurl = imgurl + ',' + fileList[key].response.url
      }
      this.dataForm.defaultImg = imgurl
    },
    handlePreview (file) {
      console.log(file)
    },
    init (id) {
      this.dataForm.houseId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.houseId) {
          this.$http({
            url: this.$http.adornUrl(`/house/renthouse/info/${this.dataForm.houseId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.housePrice = data.rentHouse.housePrice
              this.dataForm.houseWay = data.rentHouse.houseWay
              this.dataForm.houseName = data.rentHouse.houseName
              this.dataForm.houseAspect = data.rentHouse.houseAspect
              this.dataForm.houseAddress = data.rentHouse.houseAddress
              this.dataForm.houseArea = data.rentHouse.houseArea
              this.dataForm.status = data.rentHouse.status
              this.dataForm.houseDesc = data.rentHouse.houseDesc
              this.dataForm.defaultImg = data.rentHouse.defaultImg
              this.dataForm.rentSort = data.rentHouse.rentSort
              this.dataForm.deadline = data.rentHouse.deadline
              this.dataForm.userId = data.rentHouse.userId
              this.dataForm.createTime = data.rentHouse.createTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/house/renthouse/${!this.dataForm.houseId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'houseId': this.dataForm.houseId || undefined,
              'housePrice': this.dataForm.housePrice,
              'houseWay': this.dataForm.houseWay,
              'houseName': this.dataForm.houseName,
              'houseAspect': this.dataForm.houseAspect,
              'houseAddress': this.dataForm.houseAddress,
              'houseArea': this.dataForm.houseArea,
              'status': this.dataForm.status,
              'houseDesc': this.dataForm.houseDesc,
              'defaultImg': this.dataForm.defaultImg,
              'rentSort': this.dataForm.rentSort,
              'deadline': this.dataForm.deadline,
              'userId': this.dataForm.userId,
              'createTime': this.dataForm.createTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
